package com.chexiehui.jc.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;

import com.aries.ui.view.radius.RadiusTextView;
import com.aries.ui.view.title.TitleBarView;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.ChooseDictGridAdapter;
import com.chexiehui.jc.adapter.ChoosePriceGridAdapter;
import com.chexiehui.jc.entity.BrandInfo;
import com.chexiehui.jc.entity.DictInfo;
import com.chexiehui.jc.entity.PriceConditionInfo;
import com.chexiehui.jc.utils.GlideUtil;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.lxj.xpopup.impl.FullScreenPopupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class CustomAssistantMorePopup extends FullScreenPopupView {

    @BindView(R.id.titleBar)
    TitleBarView titleBar;
    @BindView(R.id.rtv_brand)
    RadiusTextView rtvBrand;
    @BindView(R.id.nsgv_price_grid)
    NonSlidingGridView nsgvPriceGrid;
    @BindView(R.id.nsgv_grade_grid)
    NonSlidingGridView nsgvGradeGrid;
    @BindView(R.id.nsgv_country_grid)
    NonSlidingGridView nsgvCountryGrid;
    @BindView(R.id.rtv_sure)
    RadiusTextView rtvSure;
    private Unbinder mUnBinder;
    private ICustomAssistantMoreListener listener;

    private List<PriceConditionInfo> priceConditionData;
    private ChoosePriceGridAdapter priceConditionAdapter;
    private List<DictInfo> gradeConditionData;
    private ChooseDictGridAdapter gradeConditionAdapter;
    private List<DictInfo> countryConditionData;
    private ChooseDictGridAdapter countryConditionAdapter;

    private BrandInfo chooseBrand;
    private PriceConditionInfo choosePrice;
    private DictInfo chooseGrade;
    private DictInfo chooseCountry;

    public CustomAssistantMorePopup(@NonNull Context context) {
        super(context);
        priceConditionData = new ArrayList<>();
        priceConditionData.add(new PriceConditionInfo("价格不限", "", ""));
        priceConditionData.add(new PriceConditionInfo("10万以下", "0", "10"));
        priceConditionData.add(new PriceConditionInfo("10-15万", "10", "15"));
        priceConditionData.add(new PriceConditionInfo("15-20万", "15", "20"));
        priceConditionData.add(new PriceConditionInfo("25-30万", "25", "30"));
        priceConditionData.add(new PriceConditionInfo("30-40万", "30", "40"));
        priceConditionData.add(new PriceConditionInfo("50万以上", "50", ""));

        gradeConditionData = new ArrayList<>();

        countryConditionData = new ArrayList<>();
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layput_custom_assistant_more_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mUnBinder = ButterKnife.bind(this);
        titleBar.setTitleMainText("更多条件")
                .setLeftTextDrawable(R.drawable.ic_svg_close)
                .setOnLeftTextClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View view) {
                        dismiss();
                    }
                });

        nsgvPriceGrid.setAdapter(priceConditionAdapter = new ChoosePriceGridAdapter(getContext(), priceConditionData));
        nsgvPriceGrid.setOnItemClickListener((parent, view, position, id) -> {
            priceConditionAdapter.changeState(position);
            choosePrice = priceConditionData.get(position);
        });
        priceConditionAdapter.changeState(-1);

        nsgvGradeGrid.setAdapter(gradeConditionAdapter = new ChooseDictGridAdapter(getContext(),
                gradeConditionData));
        nsgvGradeGrid.setOnItemClickListener((parent, view, position, id) -> {
            gradeConditionAdapter.changeState(position);
            chooseGrade = gradeConditionData.get(position);
        });
        gradeConditionAdapter.changeState(-1);

        nsgvCountryGrid.setAdapter(countryConditionAdapter = new ChooseDictGridAdapter(getContext(),
                countryConditionData));
        nsgvCountryGrid.setOnItemClickListener((parent, view, position, id) -> {
            countryConditionAdapter.changeState(position);
            chooseCountry = countryConditionData.get(position);
        });
        countryConditionAdapter.changeState(-1);
    }

    public void initModel(BrandInfo chooseBrand,
                          PriceConditionInfo choosePrice,
                          DictInfo chooseGrade,
                          DictInfo chooseCountry, List<DictInfo> gradeList, List<DictInfo> countryList) {
        this.chooseBrand = chooseBrand;
        this.choosePrice = choosePrice;
        this.chooseGrade = chooseGrade;
        this.chooseCountry = chooseCountry;
        if(gradeList != null){
            this.gradeConditionData.clear();
            this.gradeConditionData.addAll(gradeList);
        }
        if(countryList != null){
            this.countryConditionData.clear();
            this.countryConditionData.addAll(countryList);
        }
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
        setPageInfo();
    }

    public void setListener(ICustomAssistantMoreListener listener) {
        this.listener = listener;
    }

    public void setPageInfo() {
        if (chooseBrand == null) {
            rtvBrand.setText("选择品牌");
        } else {
            rtvBrand.setText("已选择：" + chooseBrand.getName());
        }
        if (choosePrice != null) {
            for (int i = 0; i < priceConditionData.size(); i++) {
                if (choosePrice.getName().equals(priceConditionData.get(i).getName())) {
                    priceConditionAdapter.changeState(i);
                    break;
                }
            }
        }
        gradeConditionAdapter.notifyDataSetChanged();
        if (chooseGrade != null) {
            for (int i = 0; i < gradeConditionData.size(); i++) {
                if (chooseGrade.getKey().equals(gradeConditionData.get(i).getKey())) {
                    gradeConditionAdapter.changeState(i);
                    break;
                }
            }
        }
        countryConditionAdapter.notifyDataSetChanged();
        if (chooseCountry != null) {
            for (int i = 0; i < countryConditionData.size(); i++) {
                if (chooseCountry.getKey().equals(countryConditionData.get(i).getKey())) {
                    countryConditionAdapter.changeState(i);
                    break;
                }
            }
        }
    }

    public void notifyBrandInfo(BrandInfo brandInfo) {
        chooseBrand = brandInfo;
        if (chooseBrand == null) {
            rtvBrand.setText("选择品牌");
        } else {
            rtvBrand.setText("已选择：" + chooseBrand.getName());
        }
    }

    public void notifyPriceInfo(PriceConditionInfo price) {
        choosePrice = price;
        if (choosePrice != null) {
            for (int i = 0; i < priceConditionData.size(); i++) {
                if (choosePrice.getName().equals(priceConditionData.get(i).getName())) {
                    priceConditionAdapter.changeState(i);
                    break;
                }
            }
        }
    }

    public void notifyGradeInfo(DictInfo grade) {
        chooseGrade = grade;
        if (chooseGrade != null) {
            for (int i = 0; i < gradeConditionData.size(); i++) {
                if (chooseGrade.getKey().equals(gradeConditionData.get(i).getKey())) {
                    gradeConditionAdapter.changeState(i);
                    break;
                }
            }
        }
    }

    public void notifyCountryInfo(DictInfo country) {
        chooseCountry = country;
        if (chooseCountry != null) {
            for (int i = 0; i < countryConditionData.size(); i++) {
                if (chooseCountry.getKey().equals(countryConditionData.get(i).getKey())) {
                    countryConditionAdapter.changeState(i);
                    break;
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }

    @OnClick({R.id.rtv_brand, R.id.rtv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rtv_brand:
                if (listener != null) {
                    listener.onAssistantMoreChooseBrand();
                }
                break;
            case R.id.rtv_sure:
                if (listener != null) {
                    listener.onAssistantMoreSure(chooseBrand, choosePrice, chooseGrade, chooseCountry);
                    dismiss();
                }
                break;
        }
    }

    public interface ICustomAssistantMoreListener {
        void onAssistantMoreChooseBrand();

        void onAssistantMoreSure(BrandInfo chooseBrand,
                                 PriceConditionInfo choosePrice,
                                 DictInfo chooseGrade,
                                 DictInfo chooseCountry);
    }

}
